package com.example.second;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.util.Log;

public class MyProvider extends ContentProvider {

    static UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);//如果匹配不成功，返回-1
    public static final int SELECT_BOOK_SUCCESS = 100;
    public static final int SELECT_USER_SUCCESS = 101;
    public static final int INSER_BOOK_SUCCESS = 200;
    public static final int INSER_USER_SUCCESS = 201;
    public static final int UPATE_BOOK_SUCCESS = 300;
    public static final int UPATE_USER_SUCCESS = 301;
    public static final int DELECT_BOOK_SUCCESS = 400;
    public static final int DELECT__USER_SUCCESS = 401;
    private DBOpenHelper helper;
    SQLiteDatabase db;
    static {
        /**
         * 静态代码块
         *
         * authority :uri中资源的存放位置（包名）；
         * path：uri中的资料路径
         * code：匹配成功后返回的匹配码
         *
         *   https://                    119.75.217.109/                 baike.html
         *
         *   content://      com.lenovo.a0518contentprovider/           path
         *
         *   com.lenovo.a0518contentprovider/select_book
         *
         * */

        matcher.addURI("com.example.myapplication", "select_book", SELECT_BOOK_SUCCESS);
        matcher.addURI("com.example.myapplication", "select_user", SELECT_USER_SUCCESS);
        matcher.addURI("com.example.myapplication", "insert_book", INSER_BOOK_SUCCESS);
        matcher.addURI("com.example.myapplication", "insert_user", INSER_USER_SUCCESS);
        matcher.addURI("com.example.myapplication", "upate_book", UPATE_BOOK_SUCCESS);
        matcher.addURI("com.example.myapplication", "upate_user", UPATE_USER_SUCCESS);
        matcher.addURI("com.example.myapplication", "delect_book", DELECT_BOOK_SUCCESS);
        matcher.addURI("com.example.myapplication", "delect_user", DELECT__USER_SUCCESS);
    }

    /**
     * 此方法只执行一次（创建的收执行）
     * 在此方法中做初始化的操作  初始化myopenhelper
     *
     * @return
     */
    @Override
    public boolean onCreate() {
        helper = new DBOpenHelper(getContext());
        db = helper.getWritableDatabase();
        return false;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        //uri 来自于内容接收者（查询者）  content://com.example.myapplication/select_book
        int code = matcher.match(uri);
        Log.d("query: code ", code + "*************");
        //1` 获取SQLiteDatabase对象
        Cursor cursor = null;

        if (code == SELECT_BOOK_SUCCESS) {
            cursor = db.query("book", projection, selection, selectionArgs, null, null, sortOrder);
        } else if (code == SELECT_USER_SUCCESS) {
            cursor = db.query("user", projection, selection, selectionArgs, null, null, sortOrder);
        } else if (code == -1) {
            throw new IllegalArgumentException("query访问路径有误，请查询");
        }
        return cursor;
    }


    @Override
    public Uri insert(Uri uri, ContentValues values) {
        int code=matcher.match(uri);
        if (code==INSER_BOOK_SUCCESS){
        long s=db.insert("book",null,values);
        if (s!=-1){
            Uri inserturi1=Uri.parse("成功添加"+s);
            return inserturi1 ;
        }else {
            throw new IllegalArgumentException("insert访问有误，请查询");}
        }

        else if (code==INSER_USER_SUCCESS){
                long user=db.insert("user",null,values);
                if (user!=-1){
                    Uri inserturi1=Uri.parse("成功添加"+user);
                    return inserturi1 ;
                }else { throw new IllegalArgumentException("insert访问有误，请查询"); }
        }
        else {
            throw new IllegalArgumentException("insert访问路径有误，请查询");
        }
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        int code=matcher.match(uri);
        int result=0;
        switch (code){
            case DELECT_BOOK_SUCCESS:
                result=db.delete("book",selection,selectionArgs);
                if (result<=0) {
                    throw new IllegalArgumentException("delect访问有误，请查询");
                }
                break;
            case DELECT__USER_SUCCESS:
                result=db.delete("user",selection,selectionArgs);
                if (result<=0) {
                    throw new IllegalArgumentException("delect访问有误，请查询");
                }
                break;
            default: throw new IllegalArgumentException("delect访问路径有误，请查询");
        }
        return 0;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        int code=matcher.match(uri);
        int result=0;
        switch (code){
            case UPATE_BOOK_SUCCESS:
                result = db.update("book", values, selection, selectionArgs);
                if (result<=0) {
                    throw new IllegalArgumentException("upate访问有误，请查询");
                }
                break;
            case UPATE_USER_SUCCESS:
                result=db.update("user",values,selection,selectionArgs);
                if (result<=0) {
                    throw new IllegalArgumentException("upate访问有误，请查询");
                }
                break;
            default: throw new IllegalArgumentException("upate访问路径有误，请查询");
        }

        return result;
    }

    @Override
    public String getType(Uri uri) {
        return null;
    }
}